package com.ymttest.business.service.shenqi;

import java.io.IOException;
import java.util.Arrays;

import com.shop2cn.iapi.cdfgeteway.parameter.EquityUnuseListReq;
import com.shop2cn.iapi.cdfgeteway.parameter.GetUserPointsReq;
import com.shop2cn.iapi.cdfgeteway.resp.PointsVo;
import com.shop2cn.iapi.cdfgeteway.service.EquityUnuseListCall;
import com.shop2cn.iapi.cdfgeteway.service.GetUserPointsCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.ZhongMianStgCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.sqlwapper.godmember.GodMemberWapper;

public class CDFMemberService {
	
	public static PointsVo getCDFMemberUserPoints(Long sellerId, Long userId) throws Exception {
		
		refreshToken();
		
		GetUserPointsReq getUserPointsReq = new GetUserPointsReq();
		GetUserPointsCall getUserPointsCall = new GetUserPointsCall();
		
		getUserPointsReq.setShopId(sellerId);
		getUserPointsReq.setUserId(userId);
		
		getUserPointsCall.setData(getUserPointsReq);
		getUserPointsCall.callService();
		
		return getUserPointsCall.getPointsVo();
	}
	
	/**
	 * 刷新中免大会员token
	 */
	public static String refreshToken() {
		
		YmatouEasyCall refreshTokenCall = new YmatouEasyCall("", "POST", "JSON");

		refreshTokenCall
				.setUrl("http://cdfgeteway.iapi.shop2cn.com/cdf/refreshToken");

		refreshTokenCall.setData("{}");

		try {
			refreshTokenCall.callService();
			
			GodMemberWapper DefGodMemberWapper = new GodMemberWapper();
			DefGodMemberWapper.updateCDFToken(ShenqiUserSet.cdfMembergoodsOwnerId);

			Logger.comment(refreshTokenCall.getReturnData());
			
//			JSONObject data = new JSONObject(refreshTokenCall.getReturnData());
			
//			return data.optString("accessToken");
			
			return "B3A90AC41EDE47878D60A257499B4A55";

		} catch (Exception e) {

			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 中免大会员发券
	 * @throws IOException 
	 */
	public static void getCdfCoupon(Integer couponType, Integer shopType, Long userId) throws IOException {

		String couponTypeString = "";

		if (couponType == 20)
			couponTypeString = "20BLD001110";
		else if (couponType == 40)
			couponTypeString = "40BLD001164";
		else
			couponTypeString = "10BLD001304";

		if (shopType == 1)
			ZhongMianStgCallService.receiveCdfCoupon(userId.intValue(),
					Arrays.asList(couponTypeString));
		else
			ZhongMianStgCallService.receiveCdfMacaoCoupon(userId.intValue(),
					Arrays.asList(couponTypeString));
	}
	
	/**
	 * 获取中免最新的一张未使用的优惠券
	 * @throws IOException 
	 */
	public static String getLatestUnuseCdfCoupon(Long userId, Long shopId) throws IOException {
		
		return ZhongMianStgCallService.getCdfCouponCode(userId.intValue(),
				shopId.intValue()).get(0);
		
//		String token = refreshToken();
			
//		String token = "2B004611AB024108A7FCADB93A756820";
//		
//		YmatouEasyCall getUnuseListCall = new YmatouEasyCall("", "POST", "STRING");
//		StringBuilder getUnuseListString = new StringBuilder();
//		
//		getUnuseListCall
//				.setUrl("https://cdfmemberapitest.cdfg.com.cn:20013/qhmcoupon/unuseList");
//		
//		getUnuseListCall.removeHeader("Content-Type");
//		getUnuseListCall.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
//		
//			
//		String jsonData = "{\"phone\": \"13100001111\"}";
//		
//		getUnuseListString.append("token=" + token + "&");
//		getUnuseListString.append("jsonData="
//				+ FormatUtil.urlEncode(jsonData.replace("\\", "")));
//		
//		getUnuseListCall.setData(getUnuseListString.toString());
//		
//		try {
//			
//			getUnuseListCall.callService();
//			Logger.comment(getUnuseListCall.getReturnData());
//
//			UnuseListResp response = null;
//
//			response = (UnuseListResp) FormatUtil.GSONToObject(new JSONObject(
//					getUnuseListCall.getReturnData()).get("data").toString(),
//					UnuseListResp.class);
//			
//			CDFCouponVo aCDFCouponVo = response.getData().get(0);
//			
//			return aCDFCouponVo.getCouNo();
//
//		} catch (Exception e) {
//			e.printStackTrace();
//			return null;
//		}
	}

	public static String getUnuseEquityNo(Long userId, Long shopId) throws Exception {

		EquityUnuseListReq equityUnuseListReq = new EquityUnuseListReq();
		EquityUnuseListCall equityUnuseListCall = new EquityUnuseListCall();

		equityUnuseListReq.setShopId(shopId);
		equityUnuseListReq.setUserId(userId);

		equityUnuseListCall.setData(equityUnuseListReq);
		equityUnuseListCall.callService();

		return equityUnuseListCall.getEquityUnuseListResp().getList().get(0).getEquityNo();
	}

	public static void main(String[] args) throws Exception {

//		CalcOrderCreditResp calcOrderCreditResp = GodPointsService.calcOrderCredit(
//				new BigDecimal("5299.00"), "CNY", 501989378L, 20238699L);
		
//		System.out.println(GodPointsService.getCDFMemberUserPoints(502060981L, 26080783L));
//		CDFMemberService.getLatestUnuseCdfCoupon();
		CDFMemberService.getUnuseEquityNo(198411140L, 500041475L);
	}
}
